function $(id) {
  return document.getElementById(id);
}
window.onload = function () {
     finaAll()
}

/***
 * 查询所有
 */
function finaAll() {
    var paramObj = new URLSearchParams();
    axios.post("/room/findAll",paramObj).then(resp =>{
        var roomStr = "";
        resp.data.forEach(e =>{
            roomStr +=`<tr><td>${e.address}</td><td>${e.accPeople}</td><td>${e.alreadyPeople}</td>
             <td>${e.statues}</td><td>${e.type}</td><td><input type="button" value="删除" onclick="delRoom(${e.id})">
             <input type="button" value="查看" onclick="findById(${e.id})"></td></tr>`
            $("bodyDate").innerHTML= roomStr;
        });
    });
}

/***
 * 显示遮罩
 */
function showDiv() {
    $("addDiv").style.visibility="visible";
    $("addDiv").style.width = document.documentElement.clientWidth+"px";
    $("addDiv").style.height = document.documentElement.clientHeight+"px";
}


/***
 * 添加房间
 */
function add() {
   axios.get("/room/add",{
       params:{
           address:$("addAddress").value,
           accPeople:$("people").value,
           type:$("type").value
       }
   }).then(resp =>{
       if(resp.data == "ok"){
           $("addDiv").style.visibility="hidden";
           finaAll()
           $("address").value=""
           $("people").value=""
           $("type").value="男生宿舍"
       }
   });
}

/**
 * 删除房间
 * @param id
 */
function delRoom(id) {
    axios.get("/room/del",{
        params:{
            id:id
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            finaAll()
        }
    });
}


/***
 * 显示查找遮罩,并显示相关信息
 */
function  findById(id) {
    $("findDiv").style.visibility="visible";
    $("findDiv").style.width = document.documentElement.clientWidth+"px";
    $("findDiv").style.height = document.documentElement.clientHeight+"px";
    axios.get("/room/findById",{
        params:{
            id:id
        }
    }).then(resp =>{
        var str =""
        var obj = resp.data;
        $("spanAddress").innerHTML=obj.address
        $("spanPeople").innerHTML=obj.accPeople
        $("spanStatues").innerHTML=obj.statues
        $("spanNum").innerHTML = obj.alreadyPeople
        $("spanType").innerHTML = obj.type


        obj.stuList.forEach(e =>{
            str +=`<tr><td>${e.name}</td><td>${e.className}</td><td>${e.phone}</td></tr>`
            $("findBody").innerHTML = str
        })
    });
}

/**
 * 动态条件查询
 */
function findByItem() {
    axios.get("/room/findByItem",{
        params:{
            address:$("findAddress").value,
            statues:$("fRoom").value,
            type:$("fType").value,
            canRoom:$("canRoom").value
        }
    }).then(resp =>{
        var str =""
        let info =resp.data.list
        info.forEach(e =>{
            str +=`<tr><td>${e.address}</td><td>${e.accPeople}</td><td>${e.alreadyPeople}</td>
             <td>${e.statues}</td><td>${e.type}</td><td><input type="button" value="删除" onclick="delRoom(${e.id})">
             <input type="button" value="查看" onclick="findById(${e.id})"></td></tr>`
            $("bodyDate").innerHTML= str;
        });
    });
}


